package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * AVA获取mac地址和cpu id（windows和linux）
 * 
 * @author admin
 *
 */
public class HardwareUtil {

	/**
	 * 获取mac地址
	 * 
	 * @return
	 */
	public static String getMACAddress() {
		String address = null;
		String os = System.getProperty("os.name");
		if (os.toLowerCase().startsWith("windows")) {
			try {
				Process p = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");
				BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
				String line;
				while ((line = br.readLine()) != null) {
					if (line.indexOf("Physical Address") > 0) {
						int index = line.indexOf(":");
						index += 2;
						address = line.substring(index).trim();
						break;
					} else if (line.indexOf("物理地址") > 0) {
						int index = line.indexOf(":");
						index += 2;
						address = line.substring(index).trim();
						break;
					}
				}
				br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (os.startsWith("Linux")) {
			try {
				Process p = Runtime.getRuntime().exec("/bin/sh -c ifconfig -a");
				BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
				String line;
				while ((line = br.readLine()) != null) {
					if (line.indexOf("HWaddr") > 0) {
						int index = line.indexOf("HWaddr") + "HWaddr".length();
						address = line.substring(index).trim();
						break;
					}
				}
				br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return address;
	}

	/**
	 * 获取CPU号,多CPU时,只取第一个
	 * 
	 * @return
	 */
	public static String getCPUSerial() {
		String result = "";
		String os = System.getProperty("os.name");
		if (os.startsWith("Windows")) {
			try {
				File file = File.createTempFile("tmp", ".vbs");
				file.deleteOnExit();
				FileWriter fw = new java.io.FileWriter(file);

				String vbs = "On Error Resume Next \r\n\r\n" + "strComputer = \".\"  \r\n"
						+ "Set objWMIService = GetObject(\"winmgmts:\" _ \r\n"
						+ "    & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n"
						+ "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\")  \r\n "
						+ "For Each objItem in colItems\r\n " + "    Wscript.Echo objItem.ProcessorId  \r\n "
						+ "    exit for  ' do the first cpu only! \r\n" + "Next                    ";

				fw.write(vbs);
				fw.close();
				Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
				BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
				String line;
				while ((line = input.readLine()) != null) {
					result += line;
				}
				input.close();
				file.delete();
				result = result.trim();
			} catch (Exception e) {
				e.fillInStackTrace();
				result = null;
			}
		} else if (os.startsWith("Linux")) {
			String CPU_ID_CMD = "dmidecode -t 4 | grep ID |sort -u |awk -F': ' '{print $2}'";
			try {
				Process p = Runtime.getRuntime().exec(new String[] { "sh", "-c", CPU_ID_CMD });// 管道
				BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
				String line;
				while ((line = br.readLine()) != null) {
					result += line;
					break;
				}
				br.close();
				result = result.trim();
			} catch (Exception e) {
				result = null;
			}
		}
		return result;
	}

	/**
	 * 获取磁盘序列号
	 * @return
	 */
	public static String getHarddiskSerial() {
		String result = "";
		String os = System.getProperty("os.name");
		if (os.startsWith("Windows")) {
			String line = "";
			try {
				Process proces = Runtime.getRuntime().exec("cmd /c dir c:");// 获取命令行参数
				BufferedReader buffreader = new BufferedReader(new InputStreamReader(proces.getInputStream(), "GBK"));
				while ((line = buffreader.readLine()) != null) {
					if (line.indexOf("卷的序列号是 ") != -1) { // 读取参数并获取硬盘序列号
						result = line.substring(line.indexOf("卷的序列号是 ") + "卷的序列号是 ".length(), line.length());
						break;
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (os.startsWith("Linux")) {
			try {
				String HARDDIS_ID_CMD = "hdparm -I /dev/sda";
				try {
					Process p = Runtime.getRuntime().exec(new String[] { "sh", "-c", HARDDIS_ID_CMD });// 管道
					BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
					String line;
					while ((line = br.readLine()) != null) {
						result += line;
						break;
					}
					br.close();
					result = result.trim();
					result = result.split(":")[3];
				} catch (Exception e) {
					result = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(getHarddiskSerial());

	}
}
